package fr.ssd.sdl.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Configurator {

	protected static final String propertiesFile = "sdl.properties";
	public static Properties properties = new Properties();

	
	protected static final String userNameKey = "store.jdbc.user";
	public static String userName = null;

	protected static final String userPasswordKey = "store.jdbc.password";
	public static String userPassword = null;

	protected static final String driverKey = "store.jdbc.driver";
	public static String driver = null;
	
	protected static final String urlKey = "store.jdbc.url";
	public static String url = null;

	protected static final String storeFileNameKey = "store.file.name";
	public static String storeFileName = null;
	
	protected static final String sourceFileNameKey = "source.file.name";
	public static String sourceFileName = null;
	
	protected static final String backendTypesKey = "store.backends";
	public static boolean fileBackend = false;
	public static boolean jdbcBackend = false;
	public static boolean terminalBackend = false;

	static {
		try {
			InputStream is = new FileInputStream(propertiesFile);
			properties.load(is);
			
			userName = getString(userNameKey);
			userPassword = getString(userPasswordKey);
			driver = getString(driverKey);
			url = getString(urlKey);
			sourceFileName = getString(sourceFileNameKey);
			storeFileName = getString(storeFileNameKey);
			
			String[] backendTypes = getString(backendTypesKey).split(",");
			for(String b : backendTypes){
				if ("jdbc".equalsIgnoreCase(b)){
					jdbcBackend = true;
				}else if ("terminal".equalsIgnoreCase(b)){
					terminalBackend = true;
				}else if ("file".equalsIgnoreCase(b)){
					fileBackend = true;
				}else{
					System.err.println(b + ": backend unknown");
					System.exit(1);
				}
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getString(String key) {
		return (String)properties.getProperty(key);
	}
}
